package org.jeecg.modules.online.config.p021c;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.AllTableColumns;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter;
import net.sf.jsqlparser.statement.select.SetOperationList;
import org.jeecg.common.util.security.AbstractQueryBlackListHandler;
import org.springframework.stereotype.Component;

@Component("onlReportQueryBlackListHandler")
/* renamed from: org.jeecg.modules.online.config.c.a */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/config/c/a.class */
public class OnlReportQueryBlackListHandler extends AbstractQueryBlackListHandler {

    /* renamed from: a */
    private static ThreadLocal<Map<String, AbstractQueryBlackListHandler.QueryTable>> f554a = new ThreadLocal<>();

    /* renamed from: b */
    private static ThreadLocal<String> f555b = new ThreadLocal<>();

    /* renamed from: b */
    private void m67b() {
        f554a.set(new HashMap(5));
        f555b.set(new String());
    }

    /* renamed from: c */
    private void m64c() {
        f554a.remove();
        f555b.remove();
    }

    /* renamed from: a */
    private void m74a(String str, AbstractQueryBlackListHandler.QueryTable queryTable) {
        f554a.get().put(str, queryTable);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* renamed from: a */
    public AbstractQueryBlackListHandler.QueryTable m76a(String str) {
        return f554a.get().get(str);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* renamed from: a */
    public void m75a(String str, String str2) {
        f554a.get().get(str).addField(str2);
    }

    private List<AbstractQueryBlackListHandler.QueryTable> getResult() {
        ArrayList arrayList = new ArrayList(f554a.get().values());
        m64c();
        return arrayList;
    }

    protected List<AbstractQueryBlackListHandler.QueryTable> getQueryTableInfo(String sql) {
        m67b();
        try {
        	Statement parse = new CCJSqlParserManager().parse(new StringReader(sql));
            if (parse instanceof Select) {
            	Select select = (Select)parse;
            	SelectBody selectBody =select.getSelectBody();
                //SetOperationList selectBody = parse.getSelectBody();
                if (selectBody instanceof PlainSelect) {
                    PlainSelect plainSelect = (PlainSelect) selectBody;
                    m71a(plainSelect);
                    m65b(plainSelect);
                }
                if (selectBody instanceof SetOperationList) {
                	SetOperationList setOperationList = (SetOperationList)selectBody;
                    List selects = setOperationList.getSelects();
                    for (int i = 0; i < selects.size(); i++) {
                        SelectBody selectBody2 = (SelectBody) selects.get(i);
                        if (selectBody2 instanceof PlainSelect) {
                            PlainSelect plainSelect2 = (PlainSelect) selectBody2;
                            m71a(plainSelect2);
                            m65b(plainSelect2);
                        }
                    }
                }
            }
            return getResult();
        } catch (JSQLParserException e) {
            e.printStackTrace();
            return null;
        }
    }

    /* renamed from: a */
    private void m71a(PlainSelect plainSelect) {
        m72a((Table) plainSelect.getFromItem());
        List<Join> joins = plainSelect.getJoins();
        if (joins != null) {
            for (Join join : joins) {
                m72a((Table) join.getRightItem());
            }
        }
    }

    /* renamed from: a */
    private void m72a(Table table) {
        String name;
        if (table.getAlias() != null) {
            name = table.getAlias().getName();
        } else {
            name = table.getName();
        }
        if (f555b.get().length() == 0) {
            f555b.set(name);
        }
        m74a(name, new AbstractQueryBlackListHandler.QueryTable(table.getName(), name));
    }

    /* renamed from: b */
    private void m65b(PlainSelect plainSelect) {
        List<SelectItem> selectItems = plainSelect.getSelectItems();
        final String str = f555b.get();
        for (SelectItem selectItem : selectItems) {
            selectItem.accept(new SelectItemVisitorAdapter() { // from class: org.jeecg.modules.online.config.c.a.1
                public void visit(SelectExpressionItem item) {
                	Expression expression = item.getExpression();
                    if (!(expression instanceof Column)) {
                        if (!OnlReportQueryBlackListHandler.this.m66b((Expression) expression)) {
                            String obj = expression.toString();
                            boolean z = false;
                            
                            for (String str2 : (OnlReportQueryBlackListHandler.f554a.get()).keySet()) {
                                if (obj.indexOf(str2 + ".") >= 0) {
                                    z = true;
                                    OnlReportQueryBlackListHandler.this.m75a(str2, obj);
                                }
                            }
                            if (!z) {
                                OnlReportQueryBlackListHandler.this.m75a(str, obj);
                                return;
                            }
                            return;
                        }
                        return;
                    }
                    Column column = (Column)expression;
                    if (column.getTable() == null) {
                        OnlReportQueryBlackListHandler.this.m75a(str, column.getColumnName());
                        return;
                    }
                    String name = column.getTable().getName();
                    AbstractQueryBlackListHandler.QueryTable m76a = (name == null || "".equals(name)) ? OnlReportQueryBlackListHandler.this.m76a(str) : OnlReportQueryBlackListHandler.this.m76a(name);
                    if (m76a != null) {
                        m76a.addField(column.getColumnName());
                    }
                }

                public void visit(AllTableColumns columns) {
                    String str2 = null;
                    try {
                        str2 = columns.getTable().getName();
                    } catch (Exception e) {
                    }
                    if (str2 == null) {
                        str2 = str;
                    }
                    OnlReportQueryBlackListHandler.this.m76a(str2).setAll(true);
                }

                public void visit(AllColumns columns) {
                    if ("*".equals(columns.toString())) {
                        OnlReportQueryBlackListHandler.this.m76a(str).setAll(true);
                    }
                }
            });
        }
    }

    /* renamed from: a */
    private boolean m73a(Expression expression) {
        if (expression != null) {
            return (expression instanceof Function) || (expression instanceof BinaryExpression) || (expression instanceof CastExpression) || (expression instanceof CaseExpression);
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* renamed from: b */
    public boolean m66b(Expression expression) {
        return (expression instanceof StringValue) || (expression instanceof DoubleValue) || (expression instanceof LongValue);
    }
}
